﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.FullTextSearch
{
    /// <summary>
    /// 
    /// </summary>
    public partial class LuceneSearchEditor : Form, IComponentEditor
    {
        /// <summary>
        /// 
        /// </summary>
        public LuceneSearchEditor()
        {
            InitializeComponent();
        }

        private void LuceneSearchEditor_Load(object sender, EventArgs e)
        {

        }

        #region Implementation of IComponentEditor

        private object _ActivityNode;
        private LuceneSearchComponent _SearchComponent;

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get
            {
                return _ActivityNode;
            }
            set
            {
                _ActivityNode = value;
            }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get
            {
                return typeof (LuceneSearchComponent);
            }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._SearchComponent = (LuceneSearchComponent) wfComponent;

            LuceneSearchInstruction instruction = (LuceneSearchInstruction) this._SearchComponent.Instruction;
            this.txt_IndexFilePath.Text = instruction.IndexingFilePath;
            this.txt_SearchResultFile.Text = instruction.SearchResultFilePath;
            this.txt_KeyField.Text = instruction.KeyField;
            this.txt_TitleField.Text = instruction.TitleField;
            this.txt_Query.Text = instruction.Query;
            this.dv_Fields.Rows.Clear();
            if(instruction.FieldWeights !=null && instruction.FieldWeights.Count>0)
            {
                foreach(string fieldName in instruction.FieldWeights.Keys)
                {
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewTextBoxCell cellFieldName=new DataGridViewTextBoxCell();
                    cellFieldName.Value = fieldName;
                    dr.Cells.Add(cellFieldName);
                    DataGridViewTextBoxCell cellWeight=new DataGridViewTextBoxCell();
                    cellWeight.Value = instruction.FieldWeights[fieldName];
                    dr.Cells.Add(cellWeight);
                    this.dv_Fields.Rows.Add(dr);
                }
            }
        }

        /// <summary></summary>
        public void SaveSettings()
        {
            LuceneSearchInstruction instruction = (LuceneSearchInstruction)this._SearchComponent.Instruction;
            instruction.IndexingFilePath = this.txt_IndexFilePath.Text;
            instruction.SearchResultFilePath = this.txt_SearchResultFile.Text;
            instruction.KeyField = this.txt_KeyField.Text;
            instruction.TitleField = this.txt_TitleField.Text;
            instruction.Query = this.txt_Query.Text;
            instruction.FieldWeights=new Dictionary<string, double>();
            foreach(DataGridViewRow dr in this.dv_Fields.Rows)
            {
                if(!dr.IsNewRow && dr.Cells[0].Value !=null)
                {
                    string fieldName = dr.Cells[0].Value.ToString();
                    double fieldWeight = double.Parse(dr.Cells[1].Value.ToString());
                    instruction.FieldWeights.Add(fieldName,fieldWeight);
                }
            }
            this._SearchComponent.Instruction = instruction;
        }

        /// <summary></summary>
        public TestResult Test()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._SearchComponent);

            this.Close();
        }

        private void btn_Browse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg=new FolderBrowserDialog();
            if(dlg.ShowDialog()==DialogResult.OK)
                this.txt_IndexFilePath.Text = dlg.SelectedPath;
        }

        private void btn_SaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg=new SaveFileDialog();
            dlg.Filter = "Search result files(*.find)|*.find|All files(*.*)|*.*)";
            if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
                this.txt_SearchResultFile.Text = dlg.FileName;
        }
    }
}
